package com.mccken.demo.classload;

import java.nio.file.Path;
import java.nio.file.WatchEvent;

import cn.hutool.core.io.watch.SimpleWatcher;
import cn.hutool.core.io.watch.WatchUtil;
import lombok.SneakyThrows;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2023-11-21 23:49
 **/
public class Temp {

	@SneakyThrows
	public static void main(String[] args) {
		
		String watcherPath = "/Users/mccken/Downloads/temp";
//		WatchUtil.createAll(watcherPath,new McckenFileSimpleWatcher())
//				.start();
		
		WatchUtil.createAll(watcherPath,new SimpleWatcher(){
			@SneakyThrows
			@Override
			public void onModify(WatchEvent event, Path currentPath) {
				System.out.println("onModify");
				McckenCompiler.run();
				run();
			}}).start();
	}

	@SneakyThrows
	private static void run() {
		TestClassLoader testClassLoader = new TestClassLoader(null);
		try {
			Class<?> clazz = testClassLoader.loadClass("com.mccken.demo.classload.Demo");
			System.out.println(clazz.getClassLoader());
			// 调用clazz的print方法
			clazz.getMethod("print").invoke(clazz.newInstance());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
